package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Catalog;
import com.ruoyi.system.service.ICatalogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 目录管理Controller
 * 
 * @author tq
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/system/catalog")
public class CatalogController extends BaseController
{
    @Autowired
    private ICatalogService catalogService;

    /**
     * 查询目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:list')")
    @GetMapping("/list")
    public TableDataInfo list(Catalog catalog)
    {
        startPage();
        List<Catalog> list = catalogService.selectCatalogList(catalog);
        return getDataTable(list);
    }

    /**
     * 导出目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:export')")
    @Log(title = "目录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Catalog catalog)
    {
        List<Catalog> list = catalogService.selectCatalogList(catalog);
        ExcelUtil<Catalog> util = new ExcelUtil<Catalog>(Catalog.class);
        util.exportExcel(response, list, "目录管理数据");
    }

    /**
     * 获取目录管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:query')")
    @GetMapping(value = "/{catalogId}")
    public AjaxResult getInfo(@PathVariable("catalogId") Long catalogId)
    {
        return success(catalogService.selectCatalogByCatalogId(catalogId));
    }

    /**
     * 新增目录管理
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:add')")
    @Log(title = "目录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Catalog catalog)
    {
        return toAjax(catalogService.insertCatalog(catalog));
    }

    /**
     * 修改目录管理
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:edit')")
    @Log(title = "目录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Catalog catalog)
    {
        return toAjax(catalogService.updateCatalog(catalog));
    }

    /**
     * 删除目录管理
     */
    @PreAuthorize("@ss.hasPermi('system:catalog:remove')")
    @Log(title = "目录管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{catalogIds}")
    public AjaxResult remove(@PathVariable Long[] catalogIds)
    {
        return toAjax(catalogService.deleteCatalogByCatalogIds(catalogIds));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Catalog> util = new ExcelUtil<Catalog>(Catalog.class);
        util.importTemplateExcel(response, "目录数据");
    }

    @Log(title = "目录管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:catalog:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Catalog> util = new ExcelUtil<Catalog>(Catalog.class);
        List<Catalog> catalogList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = catalogService.importCatalog(catalogList, updateSupport, operName);
        return success(message);
    }
}
